package cn.stylefeng.guns.modular.bshuser.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.HttpService;
import cn.stylefeng.guns.modular.bshuser.entity.BshUsers;
import cn.stylefeng.guns.modular.bshuser.mapper.BshUsersMapper;
import cn.stylefeng.guns.modular.bshuser.model.params.BshUsersParam;
import cn.stylefeng.guns.modular.bshuser.model.result.BshUsersResult;
import  cn.stylefeng.guns.modular.bshuser.service.BshUsersService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 宝山用户 服务实现类
 * </p>
 *
 * @author Dong
 * @since 2023-10-16
 */
@Service
public class BshUsersServiceImpl extends ServiceImpl<BshUsersMapper, BshUsers> implements BshUsersService {

    @Override
    public void add(BshUsersParam param){
        BshUsers entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BshUsersParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BshUsersParam param){
        BshUsers oldEntity = getOldEntity(param);
        BshUsers newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BshUsersResult findBySpec(BshUsersParam param){
        return null;
    }

    @Override
    public List<BshUsersResult> findListBySpec(BshUsersParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BshUsersParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public BshUsers getUserByToken(String token, String siteId) {
        JSONObject result = null;
        String url = "https://bsapi.shmedia.tech/media-basic-port/api/app/personal/get";
        Map<String,String> params = new HashMap<String,String>();
        params.put("token",token);
        params.put("siteId",siteId);

        try {
            result = HttpService.sendJsonPost(url, params);

            BshUsers user = this.getOne(new QueryWrapper<BshUsers>().eq("user_id",result.getString("id")));

            if(user == null){
                user = new BshUsers();
                user.setUserId(result.getString("id"));
                user.setMobile(result.getString("mobile"));
                user.setUserName(result.getString("name"));
                user.setNickName(result.getString("nickname"));
                this.save(user);
            }else{
                user.setUserName(result.getString("name"));
                user.setNickName(result.getString("nickname"));
                this.updateById(user);
            }
            return user;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private Serializable getKey(BshUsersParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BshUsers getOldEntity(BshUsersParam param) {
        return this.getById(getKey(param));
    }

    private BshUsers getEntity(BshUsersParam param) {
        BshUsers entity = new BshUsers();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
